#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "table_symboles.h"

s_table *create_table() {
    s_table *r_table = NULL;
    r_table = malloc(sizeof(s_table));

    if(r_table != NULL) {
        r_table->f_cell = NULL;
        r_table->l_cell = NULL;
        r_table->size = 0;
    }

    return r_table;
}

void delete_table(s_table *a_table) {
    if(a_table != NULL) {
        int i;

        s_cell *cur_cell = a_table->f_cell;
        s_cell *tmp_cell = NULL;

        while(cur_cell != NULL) {
            tmp_cell = cur_cell->next;

            free(cur_cell);

            cur_cell = tmp_cell;
        }

        free(a_table);
    }
}

s_cell *add_cell(s_table *a_table, char *name, int type) {
    s_cell *new_cell = NULL;

    if(a_table != NULL) {
        if(!strcmp(name, "") || (new_cell = find_cell(a_table, name)) == NULL) {
            new_cell = malloc(sizeof(s_cell));

            if(a_table->f_cell == NULL) {
                a_table->f_cell = new_cell;
            }

            if(a_table->l_cell == NULL) {
                new_cell->index = 0;
            }
            else {
                new_cell->index = (a_table->l_cell->index) + 1;
                a_table->l_cell->next = new_cell;
            }

            new_cell->name = strdup(name);
            new_cell->type = type;
            new_cell->next = NULL;

            a_table->l_cell = new_cell;
        }
    }
    return new_cell;
}

s_cell *find_cell(s_table *a_table, char *name) {
    s_cell *r_cell = NULL;

    if(a_table != NULL && strcmp(name, "")) {
        int found = 0;
        s_cell *cur_cell = a_table->f_cell;

        while(cur_cell != NULL && !found) {
            if(!strcmp(cur_cell->name, name)) {
                found = 1;
                r_cell = cur_cell;
            }

            cur_cell = cur_cell->next;;
        }
    }

    return r_cell;
}

void write_table(s_table *a_table, FILE *out) {
    s_cell *r_cell = NULL;

    if(a_table != NULL) {
        int found = 0;
        s_cell *cur_cell = a_table->f_cell;

        while(cur_cell != NULL) {
            fprintf(out, "\t%s -> %d\n", cur_cell->name, cur_cell->index);
            cur_cell = cur_cell->next;;
        }
    }
}
void write_string(s_table *a_table, FILE *out) {
    s_cell *r_cell = NULL;

    if(a_table != NULL) {
        int found = 0;
        s_cell *cur_cell = a_table->f_cell;


        while(cur_cell != NULL) {
            fprintf(out, "string%d: .asciiz %s\n", cur_cell->index, cur_cell->chaine);
            cur_cell = cur_cell->next;;
        }
        fprintf(out, "\n");
    }
}
void write_pointeur(s_table *a_table, FILE *out) {
    s_cell *r_cell = NULL;

    if(a_table != NULL) {
        int found = 0;
        s_cell *cur_cell = a_table->f_cell;


        fprintf(out, "pointeur:");
        if(cur_cell != NULL) {
            fprintf(out, " .word %d", cur_cell->entier);
            cur_cell = cur_cell->next;;
        }
        while(cur_cell != NULL) {
            fprintf(out, ", %d", cur_cell->entier);
            cur_cell = cur_cell->next;;
        }
        fprintf(out, "\n");
    }
}
void write_int(s_table *a_table, FILE *out) {
    s_cell *r_cell = NULL;

    if(a_table != NULL) {
        int found = 0;
        s_cell *cur_cell = a_table->f_cell;


        fprintf(out, "int:");
        if(cur_cell != NULL) {
            fprintf(out, " .word %d", cur_cell->entier);
            cur_cell = cur_cell->next;;
        }
        while(cur_cell != NULL) {
            fprintf(out, ", %d", cur_cell->entier);
            cur_cell = cur_cell->next;;
        }
        fprintf(out, "\n");
    }
}
void write_float(s_table *a_table, FILE *out) {
    s_cell *r_cell = NULL;

    if(a_table != NULL) {
        int found = 0;
        s_cell *cur_cell = a_table->f_cell;


        fprintf(out, "float:");
        if(cur_cell != NULL) {
            fprintf(out, " .float %f", cur_cell->reel);
            cur_cell = cur_cell->next;;
        }
        while(cur_cell != NULL) {
            fprintf(out, ", %f", cur_cell->reel);
            cur_cell = cur_cell->next;;
        }
        fprintf(out, "\n");
    }
}

void print_table(s_table *a_table) {
    s_cell *r_cell = NULL;

    if(a_table != NULL) {
        printf("\n\n\n******* Table des symboles *******\nindex     chaine\n");
        int found = 0;
        s_cell *cur_cell = a_table->f_cell;

        while(cur_cell != NULL) {
            printf("%d          %s\n", cur_cell->index, cur_cell->name);
            cur_cell = cur_cell->next;;
        }
    }
}
